/**
 * Copyright &copy; 2017 <a href="https://gitee.com/hackempire/emsite-parent">emsite</a> All rights reserved.
 */
package com.empire.emsite.common.web;

import javax.servlet.ServletConfig;
import javax.servlet.http.HttpServletRequest;

import com.ckfinder.connector.configuration.Configuration;
import com.ckfinder.connector.data.AccessControlLevel;
import com.ckfinder.connector.utils.AccessControlUtil;
import com.empire.emsite.common.config.MainConfManager;
import com.empire.emsite.common.utils.FileUtils;
import com.empire.emsite.modules.sys.security.SystemAuthorizingRealm.Principal;
import com.empire.emsite.modules.sys.utils.UserUtils;

/**
 * 类CKFinderConfig.java的实现描述：CKFinder配置
 * 
 * @author arron 2017年10月30日 下午7:07:20
 */
public class CKFinderConfig extends Configuration {

    public CKFinderConfig(ServletConfig servletConfig) {
        super(servletConfig);
    }

    @Override
    protected Configuration createConfigurationInstance() {
        Principal principal = (Principal) UserUtils.getPrincipal();
        if (principal == null) {
            return new CKFinderConfig(this.servletConf);
        }
        boolean isView = true;//UserUtils.getSubject().isPermitted("cms:ckfinder:view");
        boolean isUpload = true;//UserUtils.getSubject().isPermitted("cms:ckfinder:upload");
        boolean isEdit = true;//UserUtils.getSubject().isPermitted("cms:ckfinder:edit");
        AccessControlLevel alc = this.getAccessConrolLevels().get(0);
        alc.setFolderView(isView);
        alc.setFolderCreate(isEdit);
        alc.setFolderRename(isEdit);
        alc.setFolderDelete(isEdit);
        alc.setFileView(isView);
        alc.setFileUpload(isUpload);
        alc.setFileRename(isEdit);
        alc.setFileDelete(isEdit);
        //		for (AccessControlLevel a : this.getAccessConrolLevels()){
        //			System.out.println(a.getRole()+", "+a.getResourceType()+", "+a.getFolder()
        //					+", "+a.isFolderView()+", "+a.isFolderCreate()+", "+a.isFolderRename()+", "+a.isFolderDelete()
        //					+", "+a.isFileView()+", "+a.isFileUpload()+", "+a.isFileRename()+", "+a.isFileDelete());
        //		}
        AccessControlUtil.getInstance(this).loadACLConfig();
        try {
            //			Principal principal = (Principal)SecurityUtils.getSubject().getPrincipal();
            //			this.baseURL = ServletContextFactory.getServletContext().getContextPath()+"/userfiles/"+principal+"/";
            this.baseURL = FileUtils.path(Servlets.getRequest().getContextPath() + MainConfManager.USERFILES_BASE_URL
                    + principal + "/");
            this.baseDir = FileUtils.path(MainConfManager.getUserfilesBaseDir() + MainConfManager.USERFILES_BASE_URL
                    + principal + "/");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return new CKFinderConfig(this.servletConf);
    }

    @Override
    public boolean checkAuthentication(final HttpServletRequest request) {
        return UserUtils.getPrincipal() != null;
    }

}
